package com.yonghui.springsecurityoauth2demo.config;


import com.yonghui.springsecurityoauth2demo.handler.CustomAuthenticationFailureHandler;
import com.yonghui.springsecurityoauth2demo.handler.CustomAuthenticationSuccessHandler;
import com.yonghui.springsecurityoauth2demo.provider.CustomAuthenticationProvider;
import com.yonghui.springsecurityoauth2demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;

/**
 * @author liyonghui
 * @description: TODO
 * @date 2021/12/5 13:35
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userDetailsService;

     @Autowired
     private CustomAuthenticationProvider customAuthenticationProvider;

    @Autowired
    private CustomAuthenticationFailureHandler customAuthenticationFailureHandler;

    @Autowired
    private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }



    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/auth/**", "/login/**", "/logout/**","/custom/**","/css/**", "/js/**","/actuator/**")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin().loginPage("/oauth/login")
                .loginProcessingUrl("/form/login")
//                .successHandler(customAuthenticationSuccessHandler)
//                .failureHandler(customAuthenticationFailureHandler) // 配置自定义的 AuthenticationFailureHandler
                .permitAll()
//                .and()
//                .requestCache()
//                .requestCache(requestCache());

        ;

    }

    @Bean
    public RequestCache requestCache() {
        return new HttpSessionRequestCache();
    }

    // @Override
    // protected  void configure(AuthenticationManagerBuilder auth) throws Exception {
    //     //        在内存中定义用户，方便测试使用
    //     auth.inMemoryAuthentication()
    //             .withUser("cj")
    //             .password(passwordEncoder().encode("123456"))
    //             .roles("USER")
    //             .and()
    //             .withUser("admin")
    //             .password(passwordEncoder().encode("123456"))
    //             .authorities("admin");
    // }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
//        auth.userDetailsService(userDetailsService)
//                .passwordEncoder(passwordEncoder());
//                //.and().authenticationProvider(customAuthenticationProvider);

        auth.authenticationProvider(customAuthenticationProvider);
    }
}
